package com.hctg.forced2sleep.service;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;

import com.actor.myandroidframework.service.ActorBaseService;
import com.blankj.utilcode.util.NotificationUtils;
import com.blankj.utilcode.util.Utils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.db.AppLockDao;

/**
 * Description: toast提醒服务
 * Author     : ldf
 * Date       : 2019/11/29 on 11:39
 */
public class ToastNoticeService extends ActorBaseService {

    public static ToastNoticeService instance;
    protected int requestCode = 5;
    private AppLockDao mDao;
    public  String                   toastContent;


//    private Handler handler;

//    private static class MyHandler extends Handler {
//        public WeakReference<ToastNoticeService> weakReference;
//        public MyHandler(ToastNoticeService toastNoticeService) {
//            weakReference = new WeakReference<>(toastNoticeService);
//        }
//
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            super.handleMessage(msg);
//            if (weakReference == null) return;
//
//            boolean sleepTime = Global.isSleepTime();
//            if (sleepTime) {
//                boolean screenLock = ScreenUtils.isScreenLock();//是否锁屏
//                if (!screenLock) {
//                    String packageName = getProcessName();
//                    if (!mDao.find(packageName)) {
//                        ToastUtils.showShort(toastContent);
//                    }
//                }
//            }
//            sendEmptyMessageDelayed(0, 1_000L);
//        }
//    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
//        handler = new MyHandler(this);
        mDao = AppLockDao.getInstance(this);
        toastContent = getResources().getString(R.string.toast_sleep);
//        handler.sendEmptyMessage(0);

        //Caused by: java.lang.IllegalArgumentException: The instance of the context must be an activity object
//        UsageStatsUtils.checkPermission(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        instance = this;
//        handler.sendEmptyMessage(1);
//        return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    protected void fitForegroundService() {
//        super.fitForegroundService();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationUtils.notify(id, new Utils.Consumer<NotificationCompat.Builder>() {
                @Override
                public void accept(NotificationCompat.Builder builder) {
                    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.item_notification);
                    Intent intent1 = new Intent(ToastNoticeService.this, ToastNoticeService.class);
                    intent1.putExtra("command","CommandPlay");
                    PendingIntent pIntent1 =  PendingIntent.getService(ToastNoticeService.this,
                            requestCode, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                    remoteViews.setOnClickPendingIntent(R.id.ll, pIntent1);
                    builder
                            .setSmallIcon(R.drawable.logo)
                            .setCustomContentView(remoteViews)
                            .setVisibility(NotificationCompat.VISIBILITY_PRIVATE);

                    //开启前台进程: 高版本要调用这句, 否则过几秒后会退出
                    startForeground(id, builder.build());
                }
            });
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        instance = null;
//        handler.removeCallbacksAndMessages(null);
    }
}
